package com.daluzy.chmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.daluzy.chmanager.MyApplication;
import com.daluzy.chmanager.R;
import com.daluzy.chmanager.bean.Students;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

public class StudentInfoActivity extends AppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_student_name)
    TextView tvStudentName;
    @BindView(R.id.tv_student_phone)
    TextView tvStudentPhone;
    @BindView(R.id.tv_student_place)
    TextView tvStudentPlace;
    @BindView(R.id.tv_student_sex)
    TextView tvStudentSex;
    @BindView(R.id.tv_student_department)
    TextView tvStudentDepartment;
    @BindView(R.id.tv_student_speciality)
    TextView tvStudentSpeciality;
    @BindView(R.id.tv_student_grade)
    TextView tvStudentGrade;
    @BindView(R.id.tv_student_class)
    TextView tvStudentClass;

    String studentId= "";
    private Students students;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_info_daluzy);
        ButterKnife.bind(this);
    }

    private void initData(){
        studentId = getIntent().getStringExtra("studentId");
        BmobQuery<Students> bmobQuery = new BmobQuery<Students>();
        bmobQuery.getObject(studentId, new QueryListener<Students>() {
            @Override
            public void done(Students object, BmobException e) {
                if(e==null){
                    MyApplication.getIntance().setStudents(object);
                    setData(object);
                }else{
                    Toast.makeText(StudentInfoActivity.this,"查询失败：" + e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    @OnClick({R.id.iv_back,R.id.tv_right})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                Intent intent = new Intent(StudentInfoActivity.this,AddStudentActivity.class);
                intent.putExtra("students",students);
                intent.putExtra("type",1);
                startActivity(intent);
                break;
        }
    }
    private void setData(Students students){
        this.students = students;
        tvStudentClass.setText(String.format("班级%s", students.getStudent_class()));
        tvStudentDepartment.setText(students.getStudent_department());
        tvStudentGrade.setText(students.getStudent_grade());
        tvStudentName.setText(students.getStudent_name());
        tvStudentPhone.setText(students.getStudent_phone());
        tvStudentPlace.setText(students.getStudent_place());
        tvStudentSex.setText(String.format("性别：%s", students.getStudent_sex()));
        tvStudentSpeciality.setText(students.getStudent_speciality());

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
